new Vue({
    el: '#app',
    data() {
        return {
            order_name: "",
            orderBill_id: "",
            crtTime: "",
            price: "",
            user_id: "",
            remark: "",
            delivery: "",
            address: "",
            contact: "",
            userGroup_name: "",
            tableData: [
            ],
            response_data: "",
            sku_sum_price: 0,
            freight: 0,
            sales_order_price: 0,
            //当前页
            currentPage: 1,
            //每页数量
            pagesize: 10,
            //总页数
            total: "",
            order_id:'${orderNo}',
        }
    },
    //实时计算
    computed: {
        //计算商品总额
        sumPrice() {
            let sumPrice = 0;
            this.tableData.forEach((item) => {
                sumPrice += Number(item.skuPrice) * Number(item.purNum);
            });
            this.sku_sum_price = sumPrice;
            return sumPrice;
        },
        //计算订单总额
        salesOrderPrice() {
            let salesOrderPrice = 0;
            salesOrderPrice = Number(this.sku_sum_price) + Number(this.freight);
            this.sales_order_price = salesOrderPrice;
            return salesOrderPrice;
        }
    },
    methods: {
        handleSizeChange(val) {
            this.pagesize = val;
        },
        CurrentChange(val) {
            this.currentPage = val;
        },
        handleCurrentChange(row, event, column) {
            console.log(row, event, column, event.currentTarget)
        },
        handleEdit(index, row) {
            console.log(index, row);
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        submitForm(formName) {
            var tableData = this.tableData;//Table 数据
            var response_tableData = JSON.parse(this.response_data.context);

            var arr_result = new Array();

            for(var i = 0;i < response_tableData.length;i++){
                var obj = response_tableData[i];
                /** 判断属性是否是行属性 */
                if(2 == obj.orderBill_value_type){

                    var arr_line = JSON.parse(obj.orderBill_value);
                    for (var j = 0; j < arr_line.length; j++) {
                        var arr_i = new Array();
                        var acr = arr_line[j];

                        var map_data = new HashMap();
                        for(var n in tableData[j]){//用javascript的for/in循环遍历对象的属性
                            map_data.put(n,tableData[j][n]);
                            console.log("122133333333333333-----------------:" + map_data);
                        }
                        for(var k = 0; k < acr.length; k++){
                            acr[k].orderBill_value = map_data.get(acr[k].templateProperty_alias);
                            console.log("dasdsadsa-----------------:" + acr[k].orderBill_value);

                            arr_i.push(acr[k]);
                        }
                        arr_result.push(arr_i);
                    }

                    obj.orderBill_value = arr_result;
                }

            }
            var obj = new Object();
            obj.orderBill_id = this.orderBill_id;
            obj.orderBill_flow = "25";
            obj.context = response_tableData;
            this.update(obj);
        },
        update(obj){
            $.ajax({
                url: '/Order/baseBillController/updateOrder.json',
                type: 'POST',
                data: JSON.stringify(obj),
                contentType: "application/json",
                async: false,
                dataType: 'json',
                success: function (result) {
                    console.log(result.data)
                    if(result.ret){
                        alert("2121165");
                    }
                }
            });
        },
        ajaxFunction(){
            var resultData = "";
            var obj = new Object();
            alert(this.order_id);
            obj.order_id = this.order_id;
            obj.customStat = "";
            $.ajax({
                url: '/order/orderDetail.json',
                type: 'POST',
                data: JSON.stringify(obj),
                contentType: "application/json",
                async: false,
                dataType: 'json',
                success: function (result) {
                    console.log(result.data)
                    if(result.ret){
                        resultData = result.data;//数据记录
                    }
                }
            });
            this.showDetailMsg(resultData);
        },
        showDetailMsg(resultData){
            this.response_data = resultData;
            var arr = JSON.parse(resultData.context);
            var mapData = new HashMap();
            for(var i = 0; i < arr.length; i++){
                var obj = arr[i];
                if(1 == obj.orderBill_value_type){//单属性
                    mapData.put(obj.templateProperty_alias,null == obj.orderBill_value?"":obj.orderBill_value);
                }else if(2 == obj.orderBill_value_type){
                    var arr_i = new Array();
                    var arr_line = JSON.parse(obj.orderBill_value);
                    for(var j = 0; j < arr_line.length; j++){
                        var acr = arr_line[j];
                        var map_line = new HashMap();//一行的Map
                        for(var k = 0 ; k < acr.length; k++){
                            var jgfhj = acr[k];
                            map_line.put(jgfhj.templateProperty_alias,null == jgfhj.orderBill_value?"":jgfhj.orderBill_value);
                        }
                        arr_i.push(map_line);
                    }
                    mapData.put(obj.templateProperty_alias,arr_i);
                }
            }

            this.order_name = mapData.get("order_name");
            this.orderBill_id = resultData.orderBill_id;
            this.crtTime = mapData.get("crtTime");
            this.price = mapData.get("price");
            this.user_id = mapData.get("user_id");
            this.remark = mapData.get("remark");
            this.delivery = mapData.get("delivery");
            this.address = mapData.get("address");
            this.contact = mapData.get("contact");
            var orderDetailList = mapData.get("orderDetaiList");

            var arr_table = new Array();
            for(var i= 0; i < orderDetailList.length;i++){
                var map = orderDetailList[i];
                var obj_table = new Object();
                obj_table.skuCode = map.get("skuCode");
                obj_table.skuName= map.get("skuName");
                obj_table.skuBrand= map.get("skuBrand");
                obj_table.skuModel = map.get("skuModel");
                obj_table.skuUnit= map.get("skuUnit");
                obj_table.modelAndParam= map.get("modelAndParam");
                obj_table.arrivalTime= map.get("arrivalTime");
                obj_table.skuPrice= map.get("skuPrice");
                obj_table.purNum= map.get("purNum");
                obj_table.supplyCycle = map.get("supplyCycle");
                arr_table.push(obj_table);
            }
            this.tableData = arr_table;
        }
    },
    /** 初始化参数的时候使用 */
    async mounted(){
        this.ajaxFunction();
        this.sumPrice();
        this.salesOrderPrice();
    }
});
